﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.IO;
using System.ServiceModel;
using WindowsService.WCF;

namespace WindowsService
{
    public partial class WinService : ServiceBase
    {
        public String _logFileName = AppDomain.CurrentDomain.BaseDirectory + @"log.txt";
        public ServiceHost _serviceHost = null; //wcf监听服务

        public WinService()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Set things in motion so your service can do its work.
        /// </summary>
        /// <param name="args"></param>
        protected override void OnStart(string[] args)
        {
            try
            {
                //System.Diagnostics.Debugger.Launch(); //启动调试
                if (File.Exists(_logFileName))
                {
                    FileInfo file = new FileInfo(_logFileName);
                    file.MoveTo(file.Directory + "\\log_bak_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".log");
                }
                OnWriteLog("Service Start.");

                #region 启动WCF
                //Host a WCF Service in a Managed Windows Service
                if (_serviceHost != null)
                {
                    _serviceHost.Close();
                }

                // Create a ServiceHost for the CalculatorService type and 
                // provide the base address.
                _serviceHost = new ServiceHost(typeof(WCFService));

                // Open the ServiceHostBase to create listeners and start 
                // listening for messages.
                _serviceHost.Open();
                //Host a WCF Service in a Managed Windows Service
                #endregion
            }
            catch (Exception exception)
            {
                OnWriteLog("OnStart: " + exception.Message + "\r\n" + exception.StackTrace);
                this.Stop();
            }
        }

        /// <summary>
        /// Stop this service.
        /// </summary>
        protected override void OnStop()
        {
            try
            {
                OnWriteLog("Service Stop.");
                //关闭内嵌的WCF服务
                if (_serviceHost != null)
                {
                    _serviceHost.Close();
                    _serviceHost = null;
                }
            }
            catch (Exception exception)
            {
                OnWriteLog("OnStop: " + exception.Message + "\r\n" + exception.StackTrace);
            }
        }

        /// <summary>
        /// Print service message
        /// </summary>
        /// <param name="message"></param>
        private void OnWriteLog(string message)
        {
            #region 打印日志
            using (StreamWriter sw = new StreamWriter(_logFileName, true))
            {
                sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + message);
            }
            #endregion
        }

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        public static void Main()
        {
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] 
			{ 
				new WinService() 
			};
            ServiceBase.Run(ServicesToRun);
        }
    }
}
